# To change this template, choose Tools | Templates
# and open the template in the editor.
#ActiveProcess: A Workflow/BPM engine on Rails that is inspired by JBPM

#activeprocess.rb

module ActiveProcess
	class ProcessDef
    has_many :nodes
    has_one :start_node
	end

	class Node
    has_many :enter_directions
    has_many :leave_directions

	  def enter execution
      execute execution
    end

	  def execute execution
      leave execution
    end
 
    def leave(execution, direction = nil)
      if direction
        direction.take execution
	    else
        @default_direction.take execution
      end
    end
  end
  class EndState < Node
    def execute execution
      execution.end
    end
  end

  class Direction
    belongs_to :from_node
    belongs_to :to_node

    def take execution
      execution.node = @to_node
      @to_node.enter execution
    end
  end

  class ProcessInst
    belongs_to :process_def
    has_one :main_execution

    def start
      @main_execution = Execution.new
      @main_execution.signal
    end

    def node
      @main_execution.node
    end

    def ended_at
      @main_execution.stop_at
    end

    class Execution
      belongs_to :process_inst
      has_many :children
      has_one :node

      attr_access :stop_at

      def initialize(node)
        @node = node
      end

      def signal
        @node.leave self
      end

      def stop
       @ended_at = DateTime.new
    end
  end

end

end
